//
//  DHNetworkManager+DHHodaNetworkManager.m
//  宏达APP
//
//  Created by DIANGHONG on 2018/4/27.
//  Copyright © 2018年 123. All rights reserved.
//

#import "DHNetworkManager+DHHodaNetworkManager.h"
#import "DHGoodListModel.h"
#import "DHPlanList.h"
#import "DHPlanInfo.h"
#import <YYModel.h>

/// 宏达app 网络请求
@implementation DHNetworkManager (DHHodaNetworkManager)

- (void)loadConfigListWithPlan_id:(NSNumber *)plan_id completion:(void(^)(BOOL isSuccess, NSArray *datas))completion{
    
    NSString *urlStr = @"http://qkvop.com/mobile/index.php?act=plan&op=member_goods_list";
    
    NSDictionary *params = @{@"key":self.account.key,
                             @"plan_id":plan_id
                             };
    
    [self requestWithMethod:MethodPost UrlString:urlStr params:params completion:^(id json, BOOL isSuccess) {
        
       // NSLog(@"%@",json[@"datas"]);
        
        if (!isSuccess) {
            completion(isSuccess,nil);
            return;
        }
        if (json[@"datas"][@"error"]) {
            completion(isSuccess,nil);
            return;
        }
        
        NSArray *configList = json[@"datas"][@"info"][@"class_list"];
        
        completion(isSuccess,configList);
    }];
}

- (void)login{
    
    NSString *urlStr = @"http://qkvop.com/mobile/index.php?act=login";
    
    NSDictionary *params = @{@"username":@"大宝",
                             @"password":@"123456",
                             @"client":@"ios"
                             };
    
    [self requestWithMethod:MethodPost UrlString:urlStr params:params completion:^(id json, BOOL isSuccess) {
        //NSLog(@"%@",);
        if (!isSuccess) {
            NSLog(@"网络请求失败");
            return ;
        }

        if (json[@"datas"][@"error"]) {
            NSLog(@"登录失败");
            return ;
        }
        self.account = [DHUserAccount yy_modelWithJSON:json[@"datas"]];
        //NSLog(@"%@",self.account.key);
        
    }];
}

/*
 文案控制器商品网络请求
 **/
- (void)requestGoodListWithKey:(NSNumber *)key order:(NSNumber *)order page:(NSNumber *)page curPage:(NSNumber *)curPage gc_id:(NSNumber *)gc_id store_id:(NSNumber *)store_id keyword:(NSString *)keyword b_id:(NSNumber *)b_id own_shop:(BOOL)own_shop gift:(BOOL)gift groupbuy:(BOOL)groupbuy xianshi:(BOOL)xianshi virtual:(BOOL)virtual a_id:(NSNumber *)a_id price_from:(NSNumber *)price_from price_to:(NSNumber *)price_to plan_id:(NSNumber *)plan_id completion:(void(^)(NSArray *goodList,NSArray *planList ,DHPlanInfo *planInfo, BOOL isSuccess))completion{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:5];
    if (curPage.integerValue > 1) {
        [params addEntriesFromDictionary:@{@"curpage":curPage}];
    }
    if (key) {
        [params setObject:key forKey:@"key"];
    }
    if (order) {
        [params setObject:order forKey:@"order"];
    }
    if (page) {
        [params setObject:page forKey:@"page"];
    }
    if (gc_id) {
        [params setObject:gc_id forKey:@"gc_id"];
    }
    if (store_id) {
        [params setObject:store_id forKey:@"store_id"];
    }
    if (keyword) {
        [params setObject:keyword forKey:@"keyword"];
    }
    if (b_id) {
        [params setObject:b_id forKey:@"b_id"];
    }
    
    [params setObject:@(own_shop) forKey:@"own_shop"];
    
    
    [params setObject:@(gift) forKey:@"gift"];
    
    [params setObject:@(groupbuy) forKey:@"groupbuy"];
    
    
    [params setObject:@(xianshi) forKey:@"xianshi"];
    
    
    [params setObject:@(virtual) forKey:@"virtual"];
    
    if (a_id) {
        [params setObject:a_id forKey:@"a_id"];
    }
    if (price_from) {
        [params setObject:price_from forKey:@"price_from"];
    }
    if (price_to) {
        [params setObject:price_to forKey:@"price_to"];
    }
    if (plan_id) {
        [params setObject:plan_id forKey:@"plan_id"];
    }
    
    NSLog(@"%@",params);
    NSString *urlStr = @"http://qkvop.com/mobile/index.php?act=goods&op=goods_list_plan_app";
    
    //发送请求
    [self requestWithMethod:MethodGet UrlString:urlStr params:params completion:^(id json, BOOL isSuccess) {
        
       // NSLog(@"%@",json);
        // 网络请求失败
        if (!isSuccess) {
            //completion(nil,isSuccess);
            completion(nil,nil,nil,isSuccess);
            return ;
        }
        
        NSDictionary *dict = json[@"datas"];
        //NSLog(@"%@",json);

        NSArray *model = [NSArray yy_modelArrayWithClass:[DHGoodListModel class] json:dict[@"goods_list"]];
        NSArray *planList = [NSArray yy_modelArrayWithClass:[DHPlanList class] json:dict[@"plan_list"]];
        DHPlanInfo *planInfo = [DHPlanInfo yy_modelWithJSON:dict[@"plan_info"]];
       // NSLog(@"%@",planInfo);
        //completion(model,isSuccess);
        completion(model ,planList,planInfo,isSuccess);

    }];
}
/**
 选用商品发送至服务器
 */
- (void)postGoodToServiceWithKey:(NSString *)key goods_id:(int)goods_id plan_id:(int)plan_id gc_id:(NSNumber *)gc_id finishedBlock:(void(^)(BOOL isSuccess, NSString *isSuccessTips))finishedBlock{
    
    NSDictionary *params = @{@"key":key,
                             @"goods_id":@(goods_id),
                             @"plan_id":@(plan_id),
                             @"gc_id":gc_id
                             };
    
    NSString *urlStr = @"http://qkvop.com/mobile/index.php?act=plan&op=add_goods";
    
    [self requestWithMethod:MethodPost UrlString:urlStr params:params completion:^(id json, BOOL isSuccess) {
        
        if (!isSuccess) {
            finishedBlock(isSuccess,@"网络出现问题...");
        }
        
        if (json[@"datas"][@"error"]) {
            finishedBlock(false,@"操作失败");
            return ;
        }
        //NSLog(@"json--%@",json[@"datas"][@"error"]);
       // NSLog(@"%@",json[@"datas"][@"msg"]);
        if (json[@"datas"][@"msg"]) {
            finishedBlock(isSuccess,json[@"datas"][@"msg"]);
        }
        
    }];
}

/**
 发送网络请求加载首页界面数据

 @param completion 完成回调
 */
- (void)loadHomeDataWithCompetion:(void(^)(NSArray *arrayList, BOOL isSuccess))completion{
    
    NSString *url = @"http://qkvop.com/mobile/";
    
   // NSMutableArray *arrayM = [NSMutableArray array];
    
    [self requestWithMethod:MethodPost UrlString:url params:nil completion:^(id json, BOOL isSuccess) {
        
        if (!isSuccess) {
            completion(nil,isSuccess);
        }
        
        NSArray<NSDictionary *> *array = json[@"datas"];
        
        
        completion(array,isSuccess);
        
    }];
}

@end
